<?php
// 本类由系统自动生成，仅供测试用途
class ListAction extends Action {
	public function index(){
		$detail = M('detail');
		
		//获取对应条件
		if(I('style')==''){
			$listStyle = $detail->order('id desc')->select();
		}else{
			$where[style] = I('style'); 
			$listStyle = $detail->where($where)->order('id desc')->select();
		}
        $comment = M('comment');
		foreach($listStyle as $key => $value){
			if(strpos($listStyle[$key]['color_img'],',')){
				$listStyle[$key]['color_img'] = explode(',',$listStyle[$key]['color_img']);
			}else{
				$listStyle[$key]['color_img'] =(array)$listStyle[$key]['color_img'];			
			}
            $whereComment['id'] = $listStyle[$key]['id'];
            $commentList = $comment->where($whereComment)->select();
            $count=count($commentList);
            $listStyle[$key]['commentNum'] = $count;
		}
		$this->assign("goodslist",$listStyle);


		//获取全部
		$listAll = $detail->order('id desc')->select();
		foreach($listAll as $key => $value){
			if(strpos($listAll[$key]['color_img'],',')){
				$listAll[$key]['color_img'] = explode(',',$listAll[$key]['color_img']);
			}else{
				$listAll[$key]['color_img'] =(array)$listAll[$key]['color_img'];			
			}
		}
		
		//热卖推荐
		shuffle($listAll);
		$this->assign('hotRecommend',$listAll);
		
		//热卖排行榜
		shuffle($listAll);
		$this->assign('hotList',$listAll);
		
		$this->display('list');
		
		
	}
	public function search(){
		$key = $_POST['key'];
		
		$where['style']=array("like","%".$key."%");
		
		$detail = M("detail");
		
		$listStyle = $detail->where($arr)->select();
		
//		$this->ajaxReturn($listStyle);
		
	
		foreach($listStyle as $key => $value){
			if(strpos($listStyle[$key]['color_img'],',')){
				$listStyle[$key]['color_img'] = explode(',',$listStyle[$key]['color_img']);
			}else{
				$listStyle[$key]['color_img'] =(array)$listStyle[$key]['color_img'];			
			}	
		}
		
		$this->assign("goodslist",$listStyle);
		
		//获取全部
		$listAll = $detail->order('id desc')->select();
		
		foreach($listAll as $key => $value){
			if(strpos($listAll[$key]['color_img'],',')){
				$listAll[$key]['color_img'] = explode(',',$listAll[$key]['color_img']);
			}else{
				$listAll[$key]['color_img'] =(array)$listAll[$key]['color_img'];			
			}	
		}
		//热卖推荐
		shuffle($listAll);
		$this->assign('hotRecommend',$listAll);
		
		//热卖排行榜
		shuffle($listAll);
		$this->assign('hotList',$listAll);
		
		$this->display('list');
	}

}